import axios from 'axios'
import store from '@/store'
const service = axios.create({
  baseURL: '/api',
  timeout: 5000
})
service.interceptors.request.use((config) => {
  if(store.getters.token){
    config.headers.Authorization = `Bearer ${store.getters.token}`
  }
  return config
}, (err) => {
  if(err.response && err.response.data && err.response.data.code === 401){
    store.dispatch('user/logout')
  }
  return Promise.reject(err) 
})
service.interceptors.request.use((config) => {
  config.headers.icode = 'helloqianduanxunlianying'
  return config
})
service.interceptors.response.use((res) => {
  const { data, message, success } = res.data
  if (success) {
    return data
  }
  return Promise.reject(new Error(message))
})
export default service
